package com.hxh.generater.springBoot.simple.gen;

import com.hxh.config.Constants;
import com.hxh.entity.Project;
import com.hxh.entity.po.ClassInfo;
import com.hxh.generater.springBoot.GenerationTableInfo;
import com.hxh.generater.springBoot.genBase.GenSystemLog;
import com.hxh.generater.springBoot.util.ProcessWriteLine;
import com.hxh.util.GenUtil;
import org.apache.commons.lang3.StringUtils;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

/**
 * @ProjectName: AICode
 * @Package: com.hxh.generater.springBoot.simple.gen
 * @ClassName: GenSimpleSystemLog
 * @Author: HXH
 * @Description:
 * @Date: 2024/5/21 15:48
 */
public class GenSimpleSystemLog implements GenSystemLog {
    /**
     * @param project    :
     * @param loginTable :
     * @param logTable   :
     * @return void
     * @author HXH
     * @description:
     * @date 2024/5/21
     **/
    @Override
    public void generationSystemLogAbout(Project project, String loginTable, String logTable, String logUserFieldName) {
        try {
            ClassInfo loginUserClassInfo = GenerationTableInfo.getTableInfo(project, null, loginTable);
            ClassInfo logServiceClassInfo = GenerationTableInfo.getTableInfo(project, null, logTable);

            String className = loginUserClassInfo.getClassName();
            String userEntityName = StringUtils.uncapitalize(className);

            String logServiceName = logServiceClassInfo.getClassName();
            logServiceName = logServiceName + "Service";
            String logServiceEntityName = StringUtils.uncapitalize(logServiceName);

            String userIdType = loginUserClassInfo.getIdNameList().get(0).getJavaType();
            String userIdName = loginUserClassInfo.getIdNameList().get(0).getJavaName();
            String userIdValue = "0";
            if ("string".equals(userIdType.toLowerCase())) {
                userIdValue = "\"0\"";
            }
            String getUserIdName = "get" + StringUtils.capitalize(userIdName);
            String getUserName = "get" + StringUtils.capitalize(logUserFieldName);
            String setUserIdName = "set" + StringUtils.capitalize(userIdName);
            String setUserName = "set" + StringUtils.capitalize(logUserFieldName);

            File classFile = GenUtil.newFile(project, "config", "SystemLogAspect");

            FileWriter fileWriter = new FileWriter(classFile);
            BufferedWriter writer = new BufferedWriter(fileWriter);

            File template = new File(Constants.templatePathPrefix + "simple\\template\\config\\SystemLogAspect");
            BufferedReader br = new BufferedReader(new FileReader(template));
            String line;

            Map<String, String> replaceWord = new HashMap<>(12);
            replaceWord.put("GroupId", project.getBackendProject().getProjectGroup());
            replaceWord.put("userClassName", className);
            replaceWord.put("userEntityName", userEntityName);
            replaceWord.put("logServiceClassName", logServiceName);
            replaceWord.put("logServiceEntityName", logServiceEntityName);
            replaceWord.put("userIdType", userIdType);
            replaceWord.put("userIdName", userIdName);
            replaceWord.put("userIdValue", userIdValue);
            replaceWord.put("getUserIdName", getUserIdName);
            replaceWord.put("getUserName", getUserName);
            replaceWord.put("setUserIdName", setUserIdName);
            replaceWord.put("setUserName", setUserName);
            ProcessWriteLine processWriteLine = new ProcessWriteLine(replaceWord);
            while ((line = br.readLine()) != null) {
                processWriteLine.writeLine(line,writer);
            }
            br.close();

            writer.close();
            fileWriter.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
